import { _decorator, Animation, AudioSource, BoxCollider2D, Component, Node, UITransform, warn } from 'cc';
import { Globals } from './Globals';
import { Util } from './Util';
import { ResourcesUtil } from './ResourcesUtil';
import { Enum_AudioClip } from './Enum';
import StorageManager from './Data/StorageManager';
const { ccclass, property } = _decorator;

@ccclass('Fire')
export class Fire extends Component {

    // // 火焰数量
    // fireNum: number = 3;
    // 伤害
    hurt: number = 30;
    isMoveing: boolean = true;
    moveDirection: number = 0;
    speed: number = 15;
    fireCoolingTime = 3; //技能冷却时间

    private _resistance = 0.1 //阻力，
    private _audioSound: AudioSource;
    protected onLoad(): void {
    }
    protected onEnable(): void {
        this.speed = 15 //速度重新赋值
    }
    start() {
        try {
            this._audioSound = this.node.getComponent(AudioSource);
            ResourcesUtil.loadAudioClip(Enum_AudioClip.Fire).then((clip) => {
                this._audioSound.clip = clip;
                if (!this._audioSound || !this._audioSound.clip) {
                    console.log("@@@ loadAudioClip err: ", Enum_AudioClip.Fire)
                    return;
                }
                StorageManager.instance.sound && this._audioSound.playOneShot(this._audioSound.clip);
            })
        } catch (err) {
            warn("fire播放音效错误！", err)
        }
    }

    update(deltaTime: number) {
        if (this.isMoveing) {
            // this.node.getComponent(BoxCollider2D).size = this.node.getComponent(UITransform).contentSize;
            Util.nodeMove(this.node, this.moveDirection, this.speed);
            this.speed -= this._resistance;
            if (this.speed <= 0) {
                Globals.putNode(this.node);
            }
        }
    }



}


